package com.xmtlxx.tms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import com.xmtlxx.common.core.domain.PageQuery;
import com.xmtlxx.common.core.page.TableDataInfo;
import com.xmtlxx.common.utils.StringUtils;
import com.xmtlxx.tms.domain.Address;
import com.xmtlxx.tms.domain.bo.AddressBo;
import com.xmtlxx.tms.domain.vo.AddressVo;
import com.xmtlxx.tms.mapper.AddressMapper;
import com.xmtlxx.tms.mapper.CarrierMapper;
import com.xmtlxx.tms.service.IAddressService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xmtlxx.system.mapper.SysAreaMapper;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 地址Service业务层处理
 *
 * @author xmtlxx
 * @date 2022-08-15
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class AddressServiceImpl implements IAddressService {

    private final AddressMapper baseMapper;

    private final SysAreaMapper areaMapper;

    private final CarrierMapper carrierMapper;
    /**
     * 查询地址
     *
     * @param id 地址主键
     * @return 地址
     */
    @Override
    public AddressVo queryById(String id){
        //return baseMapper.selectVoById(id);
        return baseMapper.selectAddressVoByID(id);
    }

    /**
     * 查询地址列表
     *
     * @param bo 地址
     * @return 地址
     */
    @Override
    public TableDataInfo<AddressVo> queryPageList(AddressBo bo, PageQuery pageQuery) {
       // LambdaQueryWrapper<Address> lqw = buildQueryWrapper(bo);
       // Page<AddressVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        Page<AddressVo> result = baseMapper.selectPageAddressList(pageQuery.build(), bo);
        return TableDataInfo.build(result);
    }

    /**
     * 查询地址列表
     *
     * @param bo 地址
     * @return 地址
     */
    @Override
    public List<AddressVo> queryList(AddressBo bo) {
       // LambdaQueryWrapper<Address> lqw = buildQueryWrapper(bo);
       // return baseMapper.selectVoList(lqw);
        return baseMapper.selectAddressList(bo);
    }

    private LambdaQueryWrapper<Address> buildQueryWrapper(AddressBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<Address> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getDeptId() != null, Address::getDeptId, bo.getDeptId());
        lqw.eq(StringUtils.isNotBlank(bo.getClientId()), Address::getClientId, bo.getClientId());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), Address::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getContacts()), Address::getContacts, bo.getContacts());
        lqw.eq(StringUtils.isNotBlank(bo.getProvince()), Address::getProvince, bo.getProvince());
        lqw.eq(StringUtils.isNotBlank(bo.getCity()), Address::getCity, bo.getCity());
        lqw.eq(StringUtils.isNotBlank(bo.getArea()), Address::getArea, bo.getArea());
        lqw.eq(StringUtils.isNotBlank(bo.getProvincecode()), Address::getProvincecode, bo.getProvincecode());
        lqw.eq(StringUtils.isNotBlank(bo.getCitycode()), Address::getCitycode, bo.getCitycode());
        lqw.eq(StringUtils.isNotBlank(bo.getAreacode()), Address::getAreacode, bo.getAreacode());
        lqw.eq(StringUtils.isNotBlank(bo.getFlag()), Address::getFlag, bo.getFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), Address::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getDelFlag()), Address::getDelFlag, bo.getDelFlag());
        return lqw;
    }

    /**
     * 新增地址
     *
     * @param bo 地址
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByBo(AddressBo bo) {
        Address add = BeanUtil.toBean(bo, Address.class);
        validEntityBeforeSave(add);

        //填充UUID
        add.setId(IdUtil.simpleUUID());
        add.setDelFlag("0");

        if(StringUtils.isNotBlank(bo.getProvincecode())){
            add.setProvince(areaMapper.selectVoById(bo.getProvincecode()).getAreaName());
        }
        if(StringUtils.isNotBlank(bo.getCitycode())){
            add.setCity(areaMapper.selectVoById(bo.getCitycode()).getAreaName());
        }
        if(StringUtils.isNotBlank(bo.getAreacode())){
            add.setArea(areaMapper.selectVoById(bo.getAreacode()).getAreaName());
        }

        add.setDeptId(carrierMapper.selectById(add.getCarrierId()).getDeptId());

        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改地址
     *
     * @param bo 地址
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateByBo(AddressBo bo) {
        Address update = BeanUtil.toBean(bo, Address.class);
        validEntityBeforeSave(update);

        if(StringUtils.isNotBlank(bo.getProvincecode())){
            update.setProvince(areaMapper.selectVoById(bo.getProvincecode()).getAreaName());
        }
        if(StringUtils.isNotBlank(bo.getCitycode())){
            update.setCity(areaMapper.selectVoById(bo.getCitycode()).getAreaName());
        }
        if(StringUtils.isNotBlank(bo.getAreacode())){
            update.setArea(areaMapper.selectVoById(bo.getAreacode()).getAreaName());
        }
        update.setDeptId(carrierMapper.selectById(update.getCarrierId()).getDeptId());
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(Address entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除地址
     *
     * @param ids 需要删除的地址主键
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteWithValidByIds(Collection<String> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
